package com.hup.joystick.view.quickMenu.setting;

import com.hup.joystick.constant.FunctionConstant.QuickMenuFunctionType;
import com.hup.joystick.model.QuickMenuFunction;
import com.hup.joystick.service.QuickMenuFunctions;
import com.hup.utils.swing.customView.HComboBox;
import com.hup.utils.swing.customView.ImageButton;
import com.hup.utils.swing.customView.KeyboardText;
import lombok.Setter;
import lombok.extern.log4j.Log4j2;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

/**
 * 功能item
 *
 * @author hugan
 * @date 2020/11/11
 */
@Log4j2
public class QMSFunctionItem extends JPanel {
    public JPanel root;
    public ImageButton btnUp;
    public ImageButton btnDown;
    public HComboBox<QuickMenuFunctionType> cbType;

    public HComboBox<QuickMenuFunction> cbInnerFunction;
    public KeyboardText txtKeyboard;

    @Setter
    private FunctionEditListener editListener;

    public QMSFunctionItem() {
        $$$setupUI$$$();
        initView();
        initListener();
    }

    private void initView() {
        ActionListener al = e -> {
            if (editListener != null)
                editListener.onMove(QMSFunctionItem.this, e.getSource() == btnUp);
        };
        btnUp.setActionListener(al);
        btnDown.setActionListener(al);
        cbType.setItemTextHandler(type -> type.desc);
        cbType.setModel(QuickMenuFunctionType.values());
        cbInnerFunction.setItemTextHandler(QuickMenuFunction::getDesc);
        cbInnerFunction.setModel(QuickMenuFunctions.getBuildInFunctions(type -> type == QuickMenuFunctionType.CMD));
    }

    private void initListener() {
        cbType.addActionListener(e -> {
            updateViewType();
            if (editListener != null) editListener.afterChangedType(cbType.getSelectedItem());
        });

        cbInnerFunction.addActionListener(e -> {
            if (editListener != null) editListener.afterEditFunction();
        });
        txtKeyboard.setKeyChangedListener(() -> {
            if (editListener != null) editListener.afterEditFunction();
        });
    }

    private void createUIComponents() {
        root = this;
    }

    /**
     * Method generated by IntelliJ IDEA GUI Designer
     * >>> IMPORTANT!! <<<
     * DO NOT edit this method OR call it in your code!
     *
     * @noinspection ALL
     */
    private void $$$setupUI$$$() {
        createUIComponents();
        root.setLayout(new GridBagLayout());
        cbType = new HComboBox();
        GridBagConstraints gbc;
        gbc = new GridBagConstraints();
        gbc.gridx = 2;
        gbc.gridy = 0;
        root.add(cbType, gbc);
        final JPanel panel1 = new JPanel();
        panel1.setLayout(new GridBagLayout());
        gbc = new GridBagConstraints();
        gbc.gridx = 3;
        gbc.gridy = 0;
        gbc.weightx = 1.0;
        gbc.fill = GridBagConstraints.BOTH;
        root.add(panel1, gbc);
        cbInnerFunction = new HComboBox();
        gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.weightx = 1.0;
        gbc.fill = GridBagConstraints.BOTH;
        panel1.add(cbInnerFunction, gbc);
        txtKeyboard = new KeyboardText();
        gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.weightx = 1.0;
        gbc.fill = GridBagConstraints.BOTH;
        panel1.add(txtKeyboard, gbc);
        btnUp = new ImageButton();
        btnUp.setIcon(new ImageIcon(getClass().getResource("/img/arrow_up.gif")));
        gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 0;
        root.add(btnUp, gbc);
        btnDown = new ImageButton();
        btnDown.setIcon(new ImageIcon(getClass().getResource("/img/arrow_down.gif")));
        gbc = new GridBagConstraints();
        gbc.gridx = 1;
        gbc.gridy = 0;
        root.add(btnDown, gbc);
    }

    /**
     * @noinspection ALL
     */
    public JComponent $$$getRootComponent$$$() { return root; }

    private void updateViewType() {
        QuickMenuFunctionType type = cbType.getSelectedItem();
        if (type == null) type = QuickMenuFunctionType.NONE;
        log.debug("type={}", type);
        cbInnerFunction.setVisible(type == QuickMenuFunctionType.CMD);
        txtKeyboard.setVisible(type == QuickMenuFunctionType.KEYBOARD);
    }

    /**
     * @param function 最后一项item是null
     */
    public void setFunction(QuickMenuFunction function) {
        QuickMenuFunctionType type = function == null ? QuickMenuFunctionType.NONE : function.type;
        cbType.setSelectedItem(type);
        updateViewType();
        if (function == null) return;
        switch (type) {
            case KEYBOARD:
                txtKeyboard.setKeyCode(function.lstKeyCode, false);
                break;
            case CMD:
                ArrayList<QuickMenuFunction> buildInFunctions = QuickMenuFunctions.getBuildInFunctions(type0 -> type0 == QuickMenuFunctionType.CMD);
                for (int i = 0, innerFunctionsSize = buildInFunctions.size(); i < innerFunctionsSize; i++) {
                    QuickMenuFunction innerFunction = buildInFunctions.get(i);
                    if (innerFunction.type == QuickMenuFunctionType.CMD && innerFunction.cmdDesc.equals(function.cmdDesc)) {
                        cbInnerFunction.setSelectedIndex(i);
                        break;
                    }
                }
                break;
        }
    }

    public QuickMenuFunctionType getType() {
        return cbType.getSelectedItem();
    }

    public List<Integer> getKeyCode() {
        return txtKeyboard.getLstKeyCode();
    }

    public interface FunctionEditListener {
        void afterChangedType(QuickMenuFunctionType type);

        void afterEditFunction();

        void onMove(QMSFunctionItem item, boolean isUp);
    }

}
